package com.unitech.drink.controller;

import com.unitech.drink.api.beans.DrinkParam;
import com.unitech.drink.api.beans.DrinkSearchParam;
import com.unitech.drink.api.beans.PagedDrinkInfo;
import com.unitech.drink.service.intf.DrinkService;
import com.unitech.drink.api.beans.DrinkInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/drinks")
@CrossOrigin    // 跨域
public class DrinkController {
    @Autowired
    private DrinkService drinkService;

    // 查询指定饮品
    @GetMapping("/{id}")
    public DrinkInfo getDrink(@PathVariable("id") Long id){
        log.info("Load drink, id={}", id);
        return drinkService.loadDrinkInfo(id);
    }

    // 查询多个饮品
    @GetMapping("/")
    public List<DrinkInfo> getDrinks(List<Long> ids){
        log.info("Load drinks, ids={}", ids);
        return drinkService.loadDrinks(ids);
    }

    // 分页查询
    @PostMapping("/search")
    public PagedDrinkInfo search(@Valid @RequestBody DrinkSearchParam request) {
        log.info("search drinks, payload={}", request);
        return drinkService.search(request);
    }

    // 创建饮品
    @PostMapping("/")
    public DrinkInfo addDrink(@Valid @RequestBody DrinkParam request) {
        log.info("Create drink: data={}", request);
        return drinkService.createDrink(request);
    }

    // 上传饮品图片
    @PostMapping("/upload")
    public String uploadOssFile(MultipartFile file){
        //获取上传的文件  通过 MultipartFile
        return drinkService.uploadDrinkImage(file); // 返回上传图片的路径
    }

    // 修改饮品
    @PutMapping("/")
    public DrinkInfo updateDrink(@Valid @RequestBody DrinkParam request) {
        log.info("update drink: data={}", request);
        return drinkService.updateDrink(request);
    }

    // 删除指定饮品
    @DeleteMapping("/{id}")
    public void deleteDrink(@PathVariable("id") Long id) {
        log.info("Delete drink, id={}", id);
        drinkService.deleteDrink(id);
    }


}
